﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Products;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;
using MpSoft.Library.BUSS.Categories;

namespace MpSoft.Library.BUSS.Products
{

    #region  Product
    public class ProductBUSS
    {
        // category, isactive, sortorder, ishome or sellgood....
        private const string PRODUCT_ALL_ISTYPICAL_KEY = "Mp.product.all-{0}-{1}";
        private const string PRODUCT_BY_ID_KEY = "Mp.product.id-{0}";

        private static void RemoveCache()
        {
            MpCache.RemoveByPattern(PRODUCT_ALL_ISTYPICAL_KEY);
            MpCache.RemoveByPattern(PRODUCT_BY_ID_KEY);
            MpCache.RemoveByPattern(CategoryBUSS.CATEGORIES_ALL_KEY);
        }

        public static int Product_Add(Product obj)
        {
            var result = ProductDAO.Product_Add(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }
      
        public static int Product_Update(Product obj)
        {
            var result = ProductDAO.Product_Update(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static int Product_Delete(int ProductID)
        {
            var result = ProductDAO.Product_Delete(ProductID);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static Product Product_ByID(int productID)
        {
            string key = string.Format(PRODUCT_BY_ID_KEY, productID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (Product)obj;
            var listProd = Product_List(productID, -1, 0, 0, false, false, false, false, 2);
            Product obj2= null;
            if (listProd != null)
                obj2 = listProd.FirstOrDefault();
            if (obj2 != null)
                MpCache.Max(key, obj2);
            return obj2;
        }

        public static List<Product> Product_IsHot(int CategoryID, int NumberRow)
        {
            return Product_List(CategoryID, 1, 2, true, false, false, false, NumberRow);
        }

        public static List<Product> Product_IsHome(int CategoryID, int NumberRow)
        {
            return Product_List(CategoryID, 1, 2, false, true, false, false, NumberRow);
        }

        public static List<Product> Product_IsSellGood(int CategoryID, int NumberRow)
        {
            return Product_List(CategoryID, 1, 2, false, false, true, false, NumberRow);
        }

        public static List<Product> Product_IsPromotion(int CategoryID, int NumberRow)
        {
            return Product_List(CategoryID, 1, 2, false, false, false, true, NumberRow);
        }

        // category, isactive, sortorder, ishome or sellgood....
        public static List<Product> Product_List(int CategoryID, int IsActive,int SortOrder,
            bool IsHot, bool IsHome, bool IsSellGood, bool IsPromotion, int NumberRow)
        {
            string key = string.Format(PRODUCT_ALL_ISTYPICAL_KEY, CategoryID, IsActive);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (List<Product>)obj;
            var listProd = Product_List(-1, CategoryID, IsActive, SortOrder, IsHot, IsHome, IsSellGood, IsPromotion, NumberRow);
            if (listProd != null)
                MpCache.Max(key, listProd);
            return listProd;
        }

        public static List<Product> Product_List(int ProductID, int CategoryID, int IsActive,int SortOrder,
            bool IsHot, bool IsHome, bool IsSellGood, bool IsPromotion, int NumberRow)
        {
            return ProductDAO.Product_List(ProductID, CategoryID, IsActive, SortOrder, IsHot, IsHome, IsSellGood, IsPromotion, NumberRow);
        }

        public static List<Product> Product_Paged(int CategoryID, int IsActive,
           int SortOrder, string Keywords, int PageIndex, int PageSize, out int TotalRows)
        {
            return ProductDAO.Product_Paged(CategoryID, IsActive, SortOrder, Keywords, PageIndex, PageSize,out TotalRows);
        }
    }
    #endregion

}
